<template>
  <el-card class="detail">
    <el-form :model="formData" :rules="rules">
      <el-row :gutter="30">
        <el-col :span="8">
          <el-form-item label="仓库编码">
            <el-input class="disable" v-model="formData.code" placeholder="请输入" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库名称" prop="name">
            <el-input clearable placeholder="请选择" v-model="formData.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库类型" prop="type">
            <el-select clearable placeholder="请选择" v-model="formData.type">
              <el-option v-for="(item, index) in warehouseTypeEnum.warehouseType" :key="index" :label="item.value" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="省/市/区" prop="location">
            <el-cascader
              :placeholder="locationPlaceholder"
              clearable
              v-model="location"
              :options="options"
              :props="{ expandTrigger: 'hover' }"
              @change="handleChange"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="8"></el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="16">
          <el-form-item label="详细地址">
            <el-input clearable v-model="formData.address" style="width:773px" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库状态" prop="status">
            <el-radio-group clearable style="width:371px" v-model="formData.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="8">
          <el-form-item label="仓库面积">
            <el-input clearable placeholder="请输入" type="text" v-model.number.trim="formData.surface">
              <template slot="append">m³</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="负责人" prop="personName">
            <el-input clearable placeholder="请选择" v-model="formData.personName">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系电话">
            <el-input clearable placeholder="请选择" v-model="formData.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="buttonBox">
      <el-button round @click="$router.go(-1)">返回</el-button>
      <el-button round type="primary" @click="submit">提交</el-button>
    </el-row>
  </el-card>
</template>

<script>
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
import warehouseTypeEnum from '@/api/constant/table'
import { addWarehouse, nextCodeFactory, queryWarehouse, updateWarehouseData } from '@/api/warehouse'
export default {
  async created () {
    this.id = this.$route.params.id
    // console.log(id)
    if (this.id === 'null') {
      // 新建
      this.add()
    } else {
      this.edit()
    }
  },
  data () {
    return {
      locationPlaceholder: '',
      warehouseTypeEnum,
      id: '',
      formData: {
        type: '',
        code: '',
        name: '',
        location: '',
        address: '',
        status: 1,
        surface: '',
        personName: '',
        phone: '',
        province: '',
        city: '',
        area: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择仓库类型', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请选择区域', trigger: 'blur' }
        ],
        personName: [
          { required: true, message: '请选择区域', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择仓库状态', trigger: 'blur' }
        ]
      },
      location: [],
      options: regionData,
      value: ''
    }
  },
  methods: {
    // 新建
    async add () {
      this.$store.dispatch('tagsView/changePage', this.$route.path)
      const { data } = await nextCodeFactory({
        name: 'CK'
      })
      console.log(data)
      this.formData.code = data
      console.log(this.$route.path)
    },
    // 编辑
    async edit () {
      console.log(this.$route)
      // 编辑
      this.$store.dispatch('tagsView/changePage', this.$route.path)
      const { data } = await queryWarehouse(this.id)
      this.formData = data
      this.formData.status = +this.formData.status
      this.locationPlaceholder = this.formData.location // 利用 placeholder 回显
      const loc = this.formData.location.split('/')
      this.location[0] = TextToCode[loc[0]].code
      this.location[1] = TextToCode[loc[0]][loc[1]].code
      this.location[2] = TextToCode[loc[0]][loc[1]][loc[2]].code
    },
    // 省市区切换
    handleChange () {
      this.formData.province = this.location[0] // 省编码
      this.formData.city = this.location[1] // 市编码
      this.formData.area = this.location[2] // 区编码
      this.formData.location = this.location.map(item => CodeToText[item]).join('/')
    },
    // 提交
    async submit () {
      if (this.$route.params.id) {
        // 修改
        await updateWarehouseData(this.formData)
        this.$message.success('修改成功')
        this.$router.push('/warehouse')
      } else {
        // 新建
        await addWarehouse(this.formData)
        this.$message.success('恭喜你，提交成功~')
        this.$router.push('/warehouse')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.detail {
  height: 509px;
  padding: 10px;
}
.buttonBox {
  padding: 25px 0 0;
  margin: 0 -30px -5px;
  border-top: 1px solid #f5efee;
  text-align: center;
  .el-button {
    width: 170px;
    margin: 0 15px;
  }
}
.el-cascader::v-deep {
  width: 773px;
  .el-input {
    .el-input__inner {
      // background: #f3ebeb;
      background: #f8f5f5;
      border-color: #f3ebeb;
      color: #b5abab;
      height: 40px;
      line-height: 40px;
      border: 1px solid #f8f5f5;
      border-radius: 6px;
      &::placeholder {
        color: #b5abab;
      }
    }
  }
}
.el-select::v-deep {
  .el-input {
    width: 371px;
    .el-input__inner {
      // background: #f3ebeb;
      background: #f8f5f5;
      border-color: #f3ebeb;
      color: #b5abab;
      height: 40px;
      line-height: 40px;
      border: 1px solid #f8f5f5;
      border-radius: 6px;
      &::placeholder {
        color: #b5abab;
      }
    }
  }
}
.el-input.disable::v-deep {
  width: 371px;
  .el-input__inner {
    background: #f3ebeb;
    border-color: #f3ebeb;
    color: #b5abab;
    height: 40px;
    line-height: 40px;
    border: 1px solid #f8f5f5;
    border-radius: 6px;
    &::placeholder {
      color: #b5abab;
    }
  }
}
.el-input::v-deep {
  width: 371px;
  .el-input__inner {
    // background: #f3ebeb;
    background: #f8f5f5;
    border-color: #f3ebeb;
    color: #b5abab;
    height: 40px;
    line-height: 40px;
    border: 1px solid #f8f5f5;
    border-radius: 6px;
    &::placeholder {
      color: #b5abab;
    }
  }
  .el-input-group__append {
    box-sizing: border-box;
    width: 40px;
    height: 40px;
    background: #e7dfdf;
    border-radius: 0 6px 6px 0;
    padding: 0 10px;
    border: 0;
  }
}
.el-form-item::v-deep {
  margin-bottom: 30px;
  .el-form-item__label {
    line-height: 18px;
    padding-bottom: 10px;
    padding-left: 5px;
    font-size: 12px;
    font-family: PingFangSC,PingFangSC-Medium;
    font-weight: 500;
    color: #887e7e;
  }
  .el-form-item__content {
    margin-left: 0!important;
  }
}
</style>
